﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Chizx.Web.summary
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["UserEntity"])
            {
                Response.Redirect("/e/Login.aspx");
            }
            //实习总结发布页面仅实习生可以访问
            if (Session["UserDegree"].ToString() == "SX"){
                //1.判断是否已经发布过实习总结
                Chizx.Model.UserModel umodel= (Chizx.Model.UserModel)Session["UserEntity"];
                Chizx.Model.SummaryModel sm = new Chizx.Model.SummaryModel();
                Chizx.BLL.SummaryBll sb = new Chizx.BLL.SummaryBll();
                sm = sb.GetModelByUID(umodel.zu_id);
                if (sm != null)
                {
                    MessageBox.Show(this, "对不起,你已经发布过实习总结！");
                }
                else
                {
                    Chizx.Model.ApplyModel am = new Chizx.Model.ApplyModel();
                    Chizx.BLL.ApplyBll ab = new Chizx.BLL.ApplyBll();

                    Chizx.Model.CompanysModel cm = new Chizx.Model.CompanysModel();
                    Chizx.BLL.CompanysBll cb = new Chizx.BLL.CompanysBll();
                    am = ab.GetModelByUID(umodel.zu_id);
                    if (am.za_cid == 0)
                    {//自主实习
                        txt_zsmtype.Text = "自主实习";
                        txtzsm_cid.Text = "无";
                        txtzsm_applydesc.Text = "无(或见申请详情)";
                    }
                    else
                    {//分配实习
                        txt_zsmtype.Text = "分配实习";
                        txtzsm_cid.Text = am.za_cid.ToString();
                        int zc_id = int.Parse(am.za_cid.ToString());
                        cm = cb.GetModel(zc_id);
                        txtzsm_applydesc.Text = cm.zcom_desc;
                    }
                    txtzsm_cid.Enabled = false;
                    txtzsm_applydesc.Enabled = false;
                    txt_zsmtype.Enabled = false;
                    txtzu_id.Text = umodel.zu_id;
                    txtzu_id.Enabled = false;
                    txtzsm_cname.Text = am.za_cname;
                    txtzsm_cname.Enabled = false;
                    txtzsm_caddress.Text = am.za_caddress;
                    txtzsm_caddress.Enabled = false;
                    apply_context.Text = am.za_desc;
                    apply_context.Enabled = false;
                }
            }else{
                Response.Redirect("/e/Login.aspx");
            }
            
        }

        		protected void btnSave_Click(object sender, EventArgs e)
		{
			
			string strErr="";
			if(this.txtzu_id.Text.Trim().Length==0)
			{
				strErr+="zu_id不能为空！\\n";	
			}
			if(!PageValidate.IsNumber(txtzsm_cid.Text))
			{
				strErr+="zsm_cid格式错误！\\n";	
			}
			if(this.txtzsm_cname.Text.Trim().Length==0)
			{
				strErr+="zsm_cname不能为空！\\n";	
			}
			if(this.txtzsm_caddress.Text.Trim().Length==0)
			{
				strErr+="zsm_caddress不能为空！\\n";	
			}
			if(this.txtzsm_applydesc.Text.Trim().Length==0)
			{
				strErr+="zsm_applydesc不能为空！\\n";	
			}
			
			if(this.txtzsm_context.Text.Trim().Length==0)
			{
				strErr+="zsm_context不能为空！\\n";	
			}
			if(this.txtzsm_attachpath.Text.Trim().Length==0)
			{
				strErr+="zsm_attachpath不能为空！\\n";	
			}
			if(this.txtzsm_check.Text.Trim().Length==0)
			{
				strErr+="zsm_check不能为空！\\n";	
			}


			if(strErr!="")
			{
				MessageBox.Show(this,strErr);
				return;
			}
			string zu_id=this.txtzu_id.Text;
            string zsm_type = this.txt_zsmtype.Text;
			int zsm_cid=int.Parse(this.txtzsm_cid.Text);
			string zsm_cname=this.txtzsm_cname.Text;
			string zsm_caddress=this.txtzsm_caddress.Text;
			string zsm_applydesc=this.txtzsm_applydesc.Text;
			int zsm_journalnum=0;
			string zsm_context=this.txtzsm_context.Text;
			string zsm_attachpath=this.txtzsm_attachpath.Text;
			string zsm_check=this.txtzsm_check.Text;

			Chizx.Model.SummaryModel model=new Chizx.Model.SummaryModel();
			model.zu_id=zu_id;
			model.zsm_type=zsm_type;
			model.zsm_cid=zsm_cid;
			model.zsm_cname=zsm_cname;
			model.zsm_caddress=zsm_caddress;
			model.zsm_applydesc=zsm_applydesc;
			model.zsm_journalnum=zsm_journalnum;
			model.zsm_context=zsm_context;
			model.zsm_attachpath=zsm_attachpath;
			model.zsm_check=zsm_check;
            model.zsm_createdate = Chizx.Common.DateHelper.getUtcNow();
			model.zsm_status="s0";

            //写入系统日志
            if (Session["UserEntity"] == null)
            {
                Response.Redirect("/e/Login.aspx");
            }
            else
            {
                Chizx.BLL.SummaryBll bll = new Chizx.BLL.SummaryBll();
                bll.Add(model);
                Chizx.Model.UserModel sbmmodel = (Chizx.Model.UserModel)Session["UserEntity"];
                Chizx.BLL.SystemLogBll sbll = new Chizx.BLL.SystemLogBll();
                Chizx.Model.SystemLogModel slm = new Chizx.Model.SystemLogModel();
                slm.zu_id = sbmmodel.zu_id;
                slm.zl_dowhen = DateTime.Now;
                slm.zl_dowhat = "提交实习总结";
                slm.zl_dotype = "3";//0:Test,1:Auth,2:Read,3:Write,4:Del,5:Inpor
                slm.zl_dodesc = "操作成功";
                sbll.Add(slm);
                Chizx.Model.MessageModel mm = new Chizx.Model.MessageModel();
                Chizx.BLL.MessageBll mb = new Chizx.BLL.MessageBll();
                mm.zu_id = zu_id;
                mm.zmsg_topic = "您成功提交了实习总结";
                mm.zmsg_context = "尊敬的用户 " + zu_id + " ,您好！你已经成功提交了实习总结！请等候指导老师的审核！";
                mm.zmsg_createdate = DateTime.Now;
                mm.zmsg_status = "m0";
                mb.Add(mm);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
            }
		}


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
